//
//  ViewController.m
//  SocketTelnet
//
//  Created by Hur Song on 12. 12. 10..
//  Copyright (c) 2012년 song. All rights reserved.
//

#define PORT 21
#define  BUFF_SIZE   1024

#import "ViewController.h"


@implementation ViewController

	@synthesize ipAddrLabel = _ipAddrLabel;
	@synthesize portLabel = _portLabel;
	@synthesize massageLabel = _massageLabel;
	@synthesize connect_client = _connect_client;
	@synthesize connectButton = _connectButton;
	@synthesize disconnectButton = _disconnectButton;

	- (void)viewDidLoad
	{
		[super viewDidLoad];
	}

	- (void)didReceiveMemoryWarning
	{
		[super didReceiveMemoryWarning];
	}


	- (IBAction)respondsToConnect:(id)sender
	{
		struct sockaddr_in socketAddr;
		NSString * connectIPAddr = _ipAddrLabel.text;
		NSUInteger portNum = [_portLabel.text integerValue];
		char   buff[BUFF_SIZE];
		
		int connect_socket = socket(PF_INET, SOCK_STREAM, 0);
		
		socketAddr.sin_len = sizeof(socketAddr);
		socketAddr.sin_family = AF_INET;
		socketAddr.sin_port = htons(portNum);
		socketAddr.sin_addr.s_addr = inet_addr([connectIPAddr cStringUsingEncoding:NSUTF8StringEncoding]);
		_connect_client = connect(connect_socket, (struct sockaddr *)&socketAddr, sizeof(socketAddr));
		
		if (_connect_client < 0)
		{
			perror("connect error ");
			self.disconnectButton.hidden = YES;
			self.connectButton.hidden = NO;
		}
		else
		{
			self.disconnectButton.hidden = NO;
			self.connectButton.hidden = YES;
			
//			recv(_connect_client, buff, BUFF_SIZE, 0);
//			self.massageLabel.text = [NSString stringWithCString:(const char*)buff encoding:NSUTF8StringEncoding];
			
			recv(_connect_client, buff, BUFF_SIZE, 0);
			self.massageLabel.text = [NSString stringWithCString:(const char*)buff encoding:NSUTF8StringEncoding];
			NSLog(@"%@",[NSString stringWithCString:(const char*)buff encoding:NSUTF8StringEncoding]);
			[_ipAddrLabel resignFirstResponder];
			[_portLabel resignFirstResponder];

//			[self  ftpRecvResponse:_connect_client data:buff];
		}
	}

	- (IBAction)respondsToDisconnect:(id)sender
	{
		close(_connect_client);
		self.disconnectButton.hidden = YES;
		self.connectButton.hidden = NO;
		self.massageLabel.text = @"";
	}


	- (IBAction)respondsToLogin:(id)sender
	{
		char   buff[BUFF_SIZE];
		
		send(_connect_client, "user song\r\n",strlen("user song\r\n"),0);
		recv(_connect_client, buff, BUFF_SIZE, 0);
		
		send(_connect_client, "pass fdsa\r\n",strlen("pass fdsa\r\n"),0);
		recv(_connect_client, buff, BUFF_SIZE, 0);
		
//		send(_connect_client, "pasv\r\n",strlen("pasv\r\n"),0);
//		recv(_connect_client, buff, BUFF_SIZE, 0);
		
		send(_connect_client, "list\r\n",strlen("list\r\n"),0);
		recv(_connect_client, buff, BUFF_SIZE, 0);
		
		send(_connect_client, "pwd\r\n",strlen("pwd\r\n"),0);
		recv(_connect_client, buff, BUFF_SIZE, 0);

	}


	- (int) ftpRecvResponse:(int)sock data:(char *) buf
	{
		int i;
		
		if (recv(sock, buf, BUFF_SIZE, 0) == -1)
		{//receive the data
			perror("recv");
			return 0;;
		}
		for(i=(strlen(buf) - 1);i>0;i--) {
			if(buf[i]=='.' || buf[i]=='\r') {
				buf[i+1]='\0';
				break;
			}
		}
		NSLog(@"%s\n",buf); //print response to the screen
		return 0;
	}



@end
